from api import log
from config import HOST


class ApiRegisterLogin:
    def __init__(self, session):
        self.session = session
        # 获取图⽚验证码
        self.__url_img_code = HOST + '/common/public/verifycode1/{}'
        # 获取短信验证码
        self.__url_msg_code = HOST + '/member/public/sendSms'
        # 注册
        self.__url_register = HOST + '/member/public/reg'
        # 注册
        self.__url_login = HOST + '/member/public/login'
        # 查看是否登录接口
        self.__url_login_status = HOST + '/member/public/islogin'

    # 获取图⽚验证码
    def api_img_code(self, random):
        log.info('正在调用获取图⽚验证码接口，请求方法{}，请求url:{}'.format('get',self.__url_img_code.format(random)))
        return self.session.get(url=self.__url_img_code.format(random))

    # 获取短信验证码
    def api_msg_code(self, phone, imgVerifyCode):
        data = {
            "phone": phone,
            "imgVerifyCode": imgVerifyCode,
            "type": "reg"
        }
        log.info('正在调用获取短信验证码接口，请求方法{}，请求url:{} 请求参数:{}'.format('post',self.__url_msg_code,data))
        return self.session.post(url=self.__url_msg_code, data=data)

    # 注册
    def api_register(self, phone, password, verifycode, phone_code):
        data = {
            "phone": phone,
            "password": password,
            "verifycode": verifycode,
            "phone_code": phone_code,
            "dy_server": "on",
            "invite_phone": ""
        }
        log.info('正在调用注册接口，请求方法{}，请求url:{} 请求参数:{}'.format('post',self.__url_register,data))
        return self.session.post(url=self.__url_register, data=data)

    # 登录
    def api_login(self, keywords='13601031111', password='test123'):
        data = {
            "keywords": keywords,
            "password": password
        }
        log.info('正在调用登录接口，请求方法{}，请求url:{} 请求参数:{}'.format('post',self.__url_login,data))
        return self.session.post(url=self.__url_login, data=data)

    # 查看是否登录接口
    def api_login_status(self):
        log.info('正在调用查看是否登录接口，请求方法{}，请求url:{}'.format('post',self.__url_login_status))
        return self.session.post(url=self.__url_login_status)
